/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Day09_Ex3_generics2;

/**
 *
 * @author Mark
 */
public class Example2 {


    /**
     * this example shows a simple generic method and also illustrates method precedence.
     */
    public static void main(String[] args) {

        Integer[] iray = {1,2,3,4};
        Character[] cray = {'M','a','r','k'};
        String[] sray = {"Mark", "Fred", "Alfred"};

        printME (iray); //Method will try to find a method that takes an Integer array.
        printME (cray); //If not, then it will look for generic.
        printME (sray); //If you keep overload method, will always be called first.

    }

    // N.B. You need to include the input type before the return type. If you don't have this it will cause a syntax error.

   //private static void printME(Integer[] iray)
    private static <T> void printME(T[] t)
    {
        System.out.println("Calling Generic Version");
        int i;
        for(i = 0; i< t.length; i++)
                System.out.print(" " + t[i]);
                System.out.println("");
    }
    
    //example of method overloading (this has precedence over the generic version!
    private static void printME(Integer[] iray)
    {
        System.out.println("Calling Integer Version");
        for (int i = 0; i < iray.length; i++) {
            System.out.print(iray[i] + " ");
        }
        System.out.println("");

    }

}
